<?php

namespace App\Admin\Extensions;

use Encore\Admin\Admin;

class CheckReceiving
{
    protected $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

    protected function script()
    {
        return <<<SCRIPT

    $('.grid-check-row-receiving').on('click', function () {
    
        console.log($(this).data('id'));
        
        $.ajax({
            method: 'post',
            url: '/admin/wait_orders/receiving',
            dataType : "json",
            data: {
                _token:LA.token,
                id: $(this).data('id'),
            },
            success: function (info) {
                console.log(info);
                if(info['code']) {
                    $.pjax.reload('#pjax-container');
                    toastr.success(info['msg']);
                }else{
                    toastr.error(info['msg']);
                }
                
            }
        });
    });

SCRIPT;
    }

    protected function render()
    {
        Admin::script($this->script());
        return "<a class='btn btn-xs btn-primary fa fa-check grid-check-row-receiving' data-id='{$this->id}'>确认收货</a>";
    }

    public function __toString()
    {
        return $this->render();
    }
}